Page({
  data: {
    isChecked: false,
    showModal:false,
    screenInfo:[],
    type:0,// 判断是否要重新筛选name，0为不需要,1为需要
    sureDepartment:[],
    sureGroup:[],
    surePost:[],
    sureSex:[],
    surePlace:[],
    sureName:[],
    // 部门
    department: [],
    userGroup:[
      {name: '一组', value: '一组'},
      {name: '二组', value: '二组'},
      {name: '三组', value: '三组'},
      {name: '四组', value: '四组'},
      {name: '五组', value: '五组'},
      {name: '六组', value: '六组'},
      {name: '七组', value: '七组'},
      {name: '八组', value: '八组'},
      {name: '九组', value: '九组'},
      {name: '十组', value: '十组'},
      {name: '十一组', value: '十一组'},
      {name: '十二组', value: '十二组'},
      {name: '十三组', value: '十三组'},
    ],
    // 职务
    post:[],
    sex:[
      {name: 0, value: '男'},
      {name: 1, value:'女'},
    ],
    // 学习地点
    place:[],
    // 存储筛选到的姓名
    advancedScreen:[],
    // 为每个人添加一个是否选中的状态
    nameChecked: [],
    url:"",
  },

  //第一次加载
  onLoad() {
    console.info("初始化")
    
    this.setData({
      url: getApp().globalData.url
    });
    this.getScreenInfo();
  },
  // 获取筛选条件
  getScreenInfo(){
    let $this = this;
    dd.httpRequest({
      url: $this.data.url+"/user/getScreenInfo",
      method: 'GET',
      data: {
      },
      success: function(res) {
        //排序
        res.data.data[0].department.sort(
          function compareFunction(param1, param2) {
            return param1.localeCompare(param2,'zh-Hans-CN', {sensitivity: 'accent'});
        }); 
        res.data.data[0].learnPlace.sort(
          function compareFunction(param1, param2) {
            return param1.localeCompare(param2,'zh-Hans-CN', {sensitivity: 'accent'});
        }); 
        res.data.data[0].userGroup.sort(
          function compareFunction(param1, param2) {
            return param1.localeCompare(param2,'zh-Hans-CN', {sensitivity: 'accent'});
        }); 

        let departmentLen = $this.data.department.length;
        $this.$spliceData({department: [0,departmentLen,...res.data.data[0].department]})
        console.log("----------")
        console.log($this.data.department)
        let postLen = $this.data.post.length;
        $this.$spliceData({post: [0,postLen,...res.data.data[0].postName]})
        let placeLen = $this.data.place.length;
        $this.$spliceData({place: [0,placeLen,...res.data.data[0].learnPlace]})
        console.log(res.data.data[0])
        let userGroupLen = $this.data.userGroup.length;
        $this.$spliceData({userGroup: [0,userGroupLen,...res.data.data[0].userGroup]})
      },
      fail: function(res) {
        dd.showToast({
          type: "exception",
          content: '操作失败，请检查网络',
          duration: 2000
        })
      }
    })
  },
  // 获取筛选的姓名条件
  getScreenName(){
    let $this = this;
    dd.httpRequest({
      url: $this.data.url+"/user/getScreenName",
      method: 'GET',
      data: {
        department:$this.data.sureDepartment[0],
        userGroup:$this.data.sureGroup[0],
        userPost:$this.data.surePost[0],
        sex:$this.data.sureSex[0],
        learnPlace:$this.data.surePlace[0],
      },
      success: function(res) {
        console.log(res.data.data);

        var list = [];
        for(var i = 0;i < res.data.data.length;i++){
          var obj = {
            name:res.data.data[i],
            isChecked:true
          }
          list.push(obj)
        }
        $this.setData({ nameChecked:list })

        let advancedScreenLen = $this.data.advancedScreen.length;
        $this.$spliceData({advancedScreen: [0,advancedScreenLen,...res.data.data]})
      },
      fail: function(res) {
        console.log(res)
        dd.showToast({
          type: "exception",
          content: '获取姓名失败，请稍后重试',
          duration: 2000
        })
      }
    })
  },
  // 打开弹框
  openModal(){
    if(this.data.type == 1 || this.data.advancedScreen.length == 0){
      this.getScreenName();
      this.setData({
        type:0
      })
    }
    this.setData({
      showModal: true
    })
  },
  // 关闭弹框
  closeModal() {
    this.setData({
      showModal: false
    })
  },
  // 重置
  reset(){
    if(!this.data.isChecked){
      this.setData({
        isChecked:true,
      });
      this.setData({
        isChecked:!this.data.isChecked,
      });
    }

    this.setData({
      type:0
    })
    this.$spliceData({advancedScreen: [0,this.data.advancedScreen.length]})
    this.$spliceData({sureDepartment: [0,this.data.sureDepartment.length]})
    this.$spliceData({sureGroup: [0,this.data.sureGroup.length]})
    this.$spliceData({surePost: [0,this.data.surePost.length]})
    this.$spliceData({sureSex: [0,this.data.sureSex.length]})
    this.$spliceData({surePlace: [0,this.data.surePlace.length]})
  },

  departmentChange(e){
    let sureDepartmentLen = this.data.sureDepartment.length;
    this.$spliceData({sureDepartment: [0,sureDepartmentLen,e.detail.value]})
    this.setData({
      type:1
    })
  },
  groupChange(e){
    this.$spliceData({sureGroup: [0,this.data.sureGroup.length,e.detail.value]})
      this.setData({
        type:1
      })
  },
  postChange(e){
    this.$spliceData({surePost: [0,this.data.surePost.length,e.detail.value]})
      this.setData({
        type:1
      })
  },
  sexChange(e){
    this.$spliceData({sureSex: [0,this.data.sureSex.length,e.detail.value]})
      this.setData({
        type:1
      })
  },
  placeChange(e){
    this.$spliceData({surePlace: [0,this.data.surePlace.length,e.detail.value]})
      this.setData({
        type:1
      })
  },

  nameChange(e){
    this.$spliceData({sureName: [0,this.data.sureName.length,...e.detail.value]})
    console.log(this.data.sureName)
    for(var i = 0;i < this.data.nameChecked.length;i++){
      var isHave = 0;
      for(var j = 0;j < e.detail.value.length;j++){
        if(this.data.nameChecked[i].name == e.detail.value[j]){
          isHave++;
        }
      }
      if(isHave == 0){
        let isChecked = 'nameChecked[' + i + '].isChecked'
        this.setData({
          [isChecked]:false,
        });
      }else{
        let isChecked = 'nameChecked[' + i + '].isChecked'
        this.setData({
          [isChecked]:true,
        });
      }
    }
    console.log(this.data.nameChecked)
  },
  // 确定筛选名称
  sureName(){
    this.closeModal();
    console.log(this.data.sureName)
  },
  // 反选
  reverseElection(){
    for(var i = 0;i < this.data.nameChecked.length;i++){
      let isChecked = 'nameChecked[' + i + '].isChecked'
      this.setData({
        [isChecked]:!this.data.nameChecked[i].isChecked,
      });
    }

    this.$spliceData({sureName: [0,this.data.sureName.length]})
    for(var j = 0;j < this.data.nameChecked.length;j++){
      if(this.data.nameChecked[j].isChecked){
        this.$spliceData({sureName: [this.data.sureName.length, 0, this.data.nameChecked[j].name]})
      }
    }
    console.log(this.data.sureName)
  },
  // 全选
  allElection(){
    for(var i = 0;i < this.data.nameChecked.length;i++){
      let isChecked = 'nameChecked[' + i + '].isChecked'
      this.setData({
        [isChecked]:true,
      });
    }
    this.$spliceData({sureName: [0,this.data.sureName.length,...this.data.advancedScreen]})
    console.log(this.data.sureName)
  },
  // 点击确定，返回课表界面
  sureScreen(){
    let $this = this;

    dd.httpRequest({
      url: $this.data.url+"/user/schedule",
      method: 'GET',
      data: {
        department:$this.data.sureDepartment[0],
        userGroup:$this.data.sureGroup[0],
        userPost:$this.data.surePost[0],
        sex:$this.data.sureSex[0],
        learnPlace:$this.data.surePlace[0],
        name:$this.data.sureName
      },
      success: function(res) {
        $this.$spliceData({screenInfo: [0,$this.data.screenInfo.length]})
        for(let i = 0;i < res.data.data.length;i++){
          $this.$spliceData({screenInfo: [0,0,res.data.data[i].ddId]})
        }
        getApp().globalData.screenInfo = $this.data.screenInfo;
        console.log("---------------------------")
        console.log($this.data.screenInfo)
        dd.showToast({
          content: "共"+$this.data.screenInfo.length+"人",
          duration: 1000,
          success(){
            dd.navigateBack();
          }
        })
      },
      fail: function(res) {
        console.log(res)
        dd.showToast({
          type: "exception",
          content: '操作失败，请检查网络',
          duration: 2000
        })
      }
    })
  }
});